package com.ewanghuiju.app.widget;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.ewanghuiju.app.R;

import java.util.Calendar;
import java.util.Date;

/**
 * @author sj  qq:751943509
 * @class describe
 * @time 2020/3/10
 */
public class CustomPickerView {

    private OptionsPickerView pvCustomOptions;

    private TimePickerView pvCustomTime;

    /***
     * 获取条件选择控件
     * @param mContext
     * @param listener
     * @return
     */
    public OptionsPickerView getCustomOptions(Context mContext, OnOptionsSureListener listener) {

        pvCustomOptions = new OptionsPickerBuilder(mContext, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                if (listener != null) {
                    listener.onOptionsSureSelect(options1, option2, options3, v);
                }
            }
        })
                .setLayoutRes(R.layout.pickerview_custom_options, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        TextView tvSubmit = v.findViewById(R.id.tv_finish);
                        TextView tvCancel = v.findViewById(R.id.tv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomOptions.returnData();
                                pvCustomOptions.dismiss();
                            }
                        });

                        tvCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomOptions.dismiss();
                            }
                        });
                    }
                }).setOutSideCancelable(false)
                .setItemVisibleCount(7)
                .build();

        return pvCustomOptions;
    }

    /**
     * 获取到时间选择控件
     *
     * @param mContext
     * @param listener
     * @return
     */
    public TimePickerView getTimePick(Context mContext, boolean[] type, OnOptionsSureListener listener) {

        pvCustomTime = new TimePickerBuilder(mContext, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                if (listener != null) {
                    listener.onTimeSureSelect(date, v);
                }

            }
        })
                .setLayoutRes(R.layout.pickerview_custom_time, new CustomListener() {

                    @Override
                    public void customLayout(View v) {
                        TextView tvSubmit = v.findViewById(R.id.tv_finish);
                        TextView tvCancel = v.findViewById(R.id.tv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.returnData();
                                pvCustomTime.dismiss();
                            }
                        });
                        tvCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.dismiss();
                            }
                        });
                    }
                })
                .setContentTextSize(18)
                .setItemVisibleCount(5)
                .setType(type)
                .setLabel("年", "月", "日", "时", "分", "秒")
                .setLineSpacingMultiplier(2.2f)
                .setTextXOffset(0, 0, 0, 0, 0, 0)
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(0xFFEEEEEE).setOutSideCancelable(false)
//                .setOutSideCancelable(false)
                .build();

        return pvCustomTime;
    }

    /**
     * 获取到时间选择控件
     *
     * @param mContext
     * @param listener
     * @return
     */
    public TimePickerView getNewTimePick(Context mContext, boolean[] type, LinearLayout layout, OnOptionsSureListener listener) {

        pvCustomTime = new TimePickerBuilder(mContext, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                if (listener != null) {
                    listener.onTimeSureSelect(date, v);
                }

            }
        })
                .setLayoutRes(R.layout.pickerview_new_custom_time, new CustomListener() {

                    @Override
                    public void customLayout(View v) {
                        TextView tvSubmit = v.findViewById(R.id.tv_finish);
                        TextView tvCancel = v.findViewById(R.id.tv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.returnData();
//                                pvCustomTime.dismiss();
                                if (listener != null) {
                                    listener.onSureOrCancel();
                                }
                            }
                        });
                        tvCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
//                                pvCustomTime.dismiss();
                                if (listener != null) {
                                    listener.onSureOrCancel();
                                }
                            }
                        });
                    }
                })
                .setContentTextSize(18)
                .setItemVisibleCount(5)
                .setType(type)
                .setLabel("年", "月", "日", "时", "分", "秒")
                .setLineSpacingMultiplier(2.2f)
                .setTextXOffset(0, 0, 0, 0, 0, 0)
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(0xFFEEEEEE).setOutSideCancelable(false)
                .setDecorView(layout)
                .build();

        return pvCustomTime;
    }


    public TimePickerView getTimePick(Context mContext, boolean[] type, Calendar startDate, Calendar endDate, OnOptionsSureListener listener) {

        pvCustomTime = new TimePickerBuilder(mContext, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                if (listener != null) {
                    listener.onTimeSureSelect(date, v);
                }

            }
        })
                .setLayoutRes(R.layout.pickerview_custom_time, new CustomListener() {

                    @Override
                    public void customLayout(View v) {
                        TextView tvSubmit = v.findViewById(R.id.tv_finish);
                        TextView tvCancel = v.findViewById(R.id.tv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.returnData();
                                pvCustomTime.dismiss();
                            }
                        });
                        tvCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.dismiss();
                            }
                        });
                    }
                })
                .setContentTextSize(18)
                .setItemVisibleCount(5)
                .setType(type)
                .setLabel("年", "月", "日", "时", "分", "秒")
                .setLineSpacingMultiplier(2.2f)
                .setTextXOffset(0, 0, 0, 0, 0, 0)
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(0xFFEEEEEE)
                .setRangDate(startDate, endDate)
//                .setOutSideCancelable(false)
                .build();

        return pvCustomTime;
    }


    public interface OnOptionsSureListener {

        void onOptionsSureSelect(int options1, int options2, int options3, View v);

        void onTimeSureSelect(Date date, View v);

        void onSureOrCancel();
    }

}
